//    Copyright (C) Kherty.  All rights reserved.
#region

using System;
using System.Windows.Input;
using OpenLS.UI.Internals;

#endregion

namespace OpenLS.UI
{
    public class ViewCommands
    {
        private static readonly RoutedUICommand[] _internalCommands = new RoutedUICommand[(int) CommandId.Last];

        public static RoutedUICommand SetPageMargin
        {
            get { return EnsureCommand(CommandId.SetMargin); }
        }

        public static RoutedUICommand SetOrientation
        {
            get { return EnsureCommand(CommandId.SetOrientation); }
        }

        public static RoutedUICommand SetPaperSize
        {
            get { return EnsureCommand(CommandId.SetPaperSize); }
        }

        public static RoutedUICommand ZoomToSelection
        {
            get { return EnsureCommand(CommandId.ZoomToSelection); }
        }

        public static RoutedUICommand SetZoom
        {
            get { return EnsureCommand(CommandId.SetZoom); }
        }


        public static RoutedCommand SetZoom100
        {
            get { return EnsureCommand(CommandId.SetZoom100); }
        }

        private static RoutedUICommand EnsureCommand(CommandId commandId)
        {
            return EnsureCommand(commandId, commandId.ToString());
        }

        private static RoutedUICommand EnsureCommand(CommandId idCommand, string propertyName,
                                                     params InputGesture[] gestures)
        {
            lock (_internalCommands.SyncRoot)
            {
                if (_internalCommands[(int) idCommand] == null)
                {
                    var text = GetUIText(idCommand) ?? "!!Missing" + idCommand;
                    var newCommand = new RoutedUICommand(text, propertyName, typeof (ViewCommands));
                    if (gestures != null)
                        foreach (var gesture in gestures)
                        {
                            newCommand.InputGestures.Add(gesture);
                        }
                    _internalCommands[(int) idCommand] = newCommand;
                }
            }
            return _internalCommands[(int) idCommand];
        }

        private static string GetUIText(CommandId idCommand)
        {
            switch (idCommand)
            {
                case ViewCommands.CommandId.SetMargin:
                    return "SetMargin";
                case CommandId.SetOrientation:
                    return "SetOrientation";
                case CommandId.SetZoom:
                    return UIStringTable.SetZoom;
                case CommandId.SetZoom100:
                    return UIStringTable.SetZoom100;
                case CommandId.ZoomToSelection:
                    return UIStringTable.ZoomToSelection;
                case CommandId.SetPaperSize:
                    return "SetPaperSize";
                default:
                    throw new NotSupportedException(idCommand.ToString());
            }
        }

        #region Nested type: CommandId

        private enum CommandId
        {
            ZoomToSelection,
            SetZoom,
            SetZoom100,
            SetPaperSize,
            SetOrientation,
            SetMargin,

            Last
        }

        #endregion
    }
}